package com.room.search.util;

import com.room.search.core.page.PageDomain;
import com.room.search.core.page.TableSupport;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.List;

/**
 * By   : ghd.bright
 * Time : 2022/2/14
 * Description:
 */
public class PageUtils {

    /**
     * 设置请求分页数据
     */
    public static Pageable startPage()
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        return PageRequest.of(pageNum,pageSize);
    }


    /**
     * 手动分页
     */
    public static <T> Page<T> listConvertToPage1(List<T> list, Pageable pageable) {
        int start = (int)pageable.getOffset();
        int end = (start + pageable.getPageSize()) > list.size() ? list.size() : (start + pageable.getPageSize());
        return new PageImpl<T>(list.subList(start, end), pageable, list.size());
    }
}
